package com.dhcc.hikari.util.queue;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dhcc.hikari.controller.NrController;
import com.dhcc.hikari.entity.NurseRevocation;
import com.dhcc.hikari.service.NurseRevocationService;

/**
 * 消费队列
 * 创建者 科帮网
 * 创建时间	2017年8月4日
 */
@Component
public class ConsumeInsertNrsQueue {
	private static final Logger logger = LoggerFactory.getLogger(ConsumeInsertNrsQueue.class);
	@Autowired
	NrController nrController;
	@Autowired
	private NurseRevocationService nurseRevocationService;
	@PostConstruct
	public void startThread() {
		ExecutorService e = Executors.newFixedThreadPool(1);// 两个大小的固定线程池
		e.submit(new PollNr(nurseRevocationService));
//		e.submit(new PollNr(nurseRevocationService));
	}

	class PollNr implements Runnable {
		NurseRevocationService nurseRevocationService;
		public PollNr(NurseRevocationService nurseRevocationService) {
			this.nurseRevocationService = nurseRevocationService;
		}

		@Override
		public void run() {
			while (true) {
				try {
					List<NurseRevocation> nurseRevocations = NrsQueue.getNrsQueue().consume();
					if (nurseRevocationService != null) {
						logger.info("剩余队列总数:{}",NrsQueue.getNrsQueue().size());
						nurseRevocationService.batchSave(nurseRevocations);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	@PreDestroy
	public void stopThread() {
		logger.info("destroy");
	}
}
